import current from '../../core/current';
import utils from '../../core/utils';
import apiKeyInfo from '../../model/common/apiKeyInfo';
import apiProvider from './apiProvider';

export default class dataInfo {
  private _conditions: string;

  public get conditions() {
    return this._conditions;
  }

  public set conditions(condition: string) {
    this._conditions = condition;
  }

  private _buildVer: string = apiProvider.Build5;

  public get buildVer() {
    return this._buildVer;
  }

  public set buildVer(buildVer: string) {
    this._buildVer = buildVer;
  }

  private _keyInfo: apiKeyInfo = apiProvider.AndroidKey;

  public get keyInfo() {
    return this._keyInfo;
  }

  public set keyInfo(keyInfo: apiKeyInfo) {
    this._keyInfo = keyInfo;
  }

  private _msTimeStamp: boolean = false;

  public get msTimeStamp() {
    return this._msTimeStamp;
  }

  public set msTimeStamp(msTimeStamp: boolean) {
    this._msTimeStamp = msTimeStamp;
  }

  constructor(condition: string, buildVer?: string, keyInfo?: apiKeyInfo, msTimeStamp?: boolean) {
    this._conditions = condition;
    if (buildVer)
      this._buildVer = buildVer;
    if (keyInfo)
      this._keyInfo = keyInfo;
    if (msTimeStamp)
      this._msTimeStamp = msTimeStamp;
  }

  public toString(): string {
    let builder = '';
    if (this._conditions != '') {
      if (this._conditions.startsWith('&'))
        this._conditions = this._conditions.substring(1, this._conditions.length - 1);
      builder = this._conditions + '&';
    }
    if (this._keyInfo == apiProvider.LoginKey) {
      builder += `&access_key=&appkey=${this._keyInfo.appkey}&platform=android&mobi_app=android&device=phone&ts=${apiProvider.timeSpanSeconds}&build=${apiProvider.Build7}`;
    } else {
      var accessKey = current.instance.accesskey;
      if (!utils.isEmpty(accessKey))
        builder += `access_key=${accessKey}&`;
      builder += "appkey=" + this._keyInfo.appkey + "&build=" + this._buildVer + "&platform=android&mobi_app=android";
      if (!this._msTimeStamp)
        builder += "&ts=" + apiProvider.timeSpanSeconds;
      else
        builder += "&ts=" + apiProvider.timeSpanMilliseconds;
    }
    builder += "&sign=" + apiProvider.getSign(builder, this._keyInfo);
    return builder;
  }
}